//
//  TrainTableViewController.swift
//  YueJian
//
//  Created by mac on 2017/10/19.
//  Copyright © 2017年 MapleStory. All rights reserved.
//

import UIKit
import Alamofire

class TrainTableViewController: UITableViewController {

    var degress = 1
    var array:[AnyObject]?
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "体育培训"
        let butItem = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(goSearchView))
        self.navigationItem.rightBarButtonItem = butItem
        loadData()
        self.tableView.register(UINib(nibName: "TrainTableViewCell", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
    }

    @objc func goSearchView() {
        let search = SearchViewController()
        search.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(search, animated: true)
    }

    func loadData() {
        let sessionId = UserDefaults.standard.string(forKey: "sessionId")
        print(degress)//主要是控制这个参数c
        Alamofire.request(URL(string: "http://172.24.10.175/workout/api.php/lists/mod/train")!, method: .post, parameters: ["SessionID":sessionId,"page":"1","Degress":degress], encoding: URLEncoding.default, headers: nil).responseJSON { (responcse) in
            print(responcse.result.value)
            self.array = responcse.result.value as! [AnyObject]
            self.tableView?.reloadData()
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc func changeData(btn:UIButton) {
        self.degress = btn.tag
        self.loadData()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if(section == 0) {
            return 1;
        } else {
            return array?.count ?? 0;
        }
       
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 240
        } else {
            return 180
        }
    }
//搜索出来的东西一样, 是因为你们接口很烂, 接口帅选不出东西 neng dongle~/你看看这几个页面. 你再看看你给我的, 我能当他三个用//这个收藏.我功能给你加上了,但是你们接口没有提交, 所有只能实现点击收藏后, 后面数据跟着变化.看见台球俱乐部了, 按说都应该有, 但是接口返回来的数据, 总是不完整,有的有有的没有//这四个按钮功能也实现了数据切换,页面刷新, 但是接口返回来全是一样的, 我也很无奈.还有, 你如果要想回到登录页````我给你价格推出吧```还有一个比较亮的一点, 你答辩的时候可以说的,这个页面的tableviewcell是实现自适应高度的,看见这个自适应了吗?这个和搜索是加分项,记得答辩时候说,还有收藏按钮的切换和后面数据的增加和减少,这个页面的金额计算功能
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! TrainTableViewCell
            cell.oneBtn.addTarget(self, action:#selector(changeData(btn:)) , for: .touchUpInside)
            cell.twoBtn.addTarget(self, action: #selector(changeData(btn:)), for: UIControlEvents.touchUpInside)
            cell.threeBtn.addTarget(self, action: #selector(changeData(btn:)), for: .touchUpInside)
            cell.fourBtn.addTarget(self, action: #selector(changeData(btn:)), for: .touchUpInside)
            return cell
        } else {
            let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
            let imageView = UIImageView(image: UIImage(named: "scroller0"))
            cell.backgroundView = imageView
            cell.textLabel?.textColor = UIColor.white
//            cell.textLabel?.text = "情侣瑜伽入门课"
            
            
            if array?.count != 0 {
                let dic = array?[indexPath.row] as! NSDictionary
                
                let str = dic["thumb"] as! String
                
                let imageStr = "http://172.24.10.175/workout/Uploads/"+str
                
                let images = try! UIImage(data: Data(contentsOf: URL(string: imageStr)!))
                
                cell.backgroundView = UIImageView(image: images)
                cell.textLabel?.text = dic["train_name"] as? String
            }
            
            return cell
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = TrainDetailTableViewController()
        let dic = array?[indexPath.row] as! NSDictionary
        vc.title = "体验中心"
        vc.hidesBottomBarWhenPushed = true
        vc.id = dic["id"] as! String
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
 

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
